package com.situ.web.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.web.entity.Student;
import com.situ.web.util.JDBCUtil;

@WebServlet("/student1")
public class StudentServlet1 extends HttpServlet {
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("StudentServlet.service()");

//			Student student1 = new Student(1, "张三", 23, "男");		
//			Student student2 = new Student(2, "李四", 21, "女");		
//			Student student3 = new Student(3, "王五", 22, "女");		
//			Student student4 = new Student(4, "赵六", 23, "男");		
//			Student student5 = new Student(5, "关羽", 20, "男");	
//			list.add(student1);
//			list.add(student2);
//			list.add(student3);
//			list.add(student4);
//			list.add(student5);

		ArrayList<Student> list = new ArrayList<Student>();
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT id,`name`,age,gender FROM student";
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				Student student = new Student(id, name, age, gender);
				list.add(student);
			}

			for (Student student : list) {
				System.out.println(student);
			}
			// 6、关闭连接
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
			// 放到一块内存区域里面
			req.setAttribute("list", list);
			// 请求转发到student_list.jsp页面进行展示
			req.getRequestDispatcher("/student_list.jsp").forward(req, resp);

			// 解决乱码问题
//			resp.setContentType("text/html;charset=utf-8");
//			PrintWriter printWriter = resp.getWriter();
//			printWriter.println("<table border='1' cellspacing='0' width='400px'>");
//			printWriter.println("	<tr>                                         ");
//			printWriter.println("		<td>ID</td>                              ");
//			printWriter.println("		<td>名字</td>                            ");
//			printWriter.println("		<td>年龄</td>                            ");
//			printWriter.println("		<td>性别</td>                            ");
//			printWriter.println("	</tr>                                        ");
//			for (Student student : list) {
//				printWriter.println("	<tr>   ");
//				printWriter.println(" <td>"+student.getId()+ "</td>  ");
//				printWriter.println("	<td>"+student.getName()+"</td>   ");
//				printWriter.println(" <td>"+student.getAge()+"</td>  ");
//				printWriter.println(" <td>"+student.getGender()+"</td>  "); 
//				printWriter.println("	</tr>   ");
//			}
//
//			printWriter.close();
	}
}